package com.tony.domain.bo;

import com.tony.domain.enumerate.ErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * Describe: 封装Json返回信息
 *
 * @author tony
 */
@Data
@ApiModel
public class JsonResult<T> {
    @ApiModelProperty("是否成功")
    private boolean success = true;

    @ApiModelProperty("返回消息")
    private String message = "succeed";

    @ApiModelProperty("返回编码")
    private int code = 200;

    @ApiModelProperty("返回数据")
    private T data;

    public JsonResult() {
    }

    public JsonResult(T data) {
        if (data != null) {
            this.data = data;
        }
    }

    public JsonResult(boolean success, String message) {
        this.success = success;
        this.code = 500;
        this.message = message;
    }

    public JsonResult(Exception exception) {
        this.success = false;
        this.code = 500;
        this.message = exception.getMessage();
    }

    public JsonResult(Exception exception, T data) {
        this.success = false;
        this.code = 500;
        this.message = exception.getMessage();
        this.data = data;
    }

    public JsonResult(ErrorEnum error) {
        this.success = false;
        this.code = error.getErrorCode();
        this.message = error.getErrorMessage();
    }

    public JsonResult(ErrorEnum error, T data) {
        this.success = false;
        this.code = error.getErrorCode();
        this.message = error.getErrorMessage();
        this.data = data;
    }

}
